package com.example.restful01.controller;

import com.example.restful01.model.BorrowRecords;
import com.example.restful01.service.BookService;
import com.example.restful01.service.BorrowRecordsService;
import com.example.restful01.service.UserService;
import com.example.restful01.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api") // 与前端axios的baseURL对应
@CrossOrigin // 跨域注解，也可以使用上面的全局配置
public class BorrowRecordsController {

    @Autowired
    private BorrowRecordsService borrowRecordsService;

    // 人员列表接口
    @GetMapping("/borrowslist")
    public Result readerInfo() {

            return Result.success(borrowRecordsService.list());


    }


    @DeleteMapping("/deleteborrow")
    public Result deleteBorrow(@RequestBody BorrowRecords borrowRecords) {
        if (borrowRecordsService.deleteBorrow(borrowRecords)) {
            return Result.success("删除借阅记录成功");
        } else {
            return Result.error("删除借阅记录失败");
        }

    }
    @PostMapping("/addborrow")
    public Result addBorrow(@RequestBody BorrowRecords borrowRecords){
        int i = borrowRecordsService.addBorrow(borrowRecords);
        if (i > 0) {
            return Result.success("添加借书成功！");
        } else {
            return Result.error("添加借书失败！");
        }
    }
    @GetMapping("/borrows")
    public Result initBorrows(@RequestParam Integer readerId) {
            return Result.success(borrowRecordsService.initBorrows(readerId));
        }

    @PutMapping("/returnbook")
    public Result returnBook(@RequestParam Integer recordId) {
        if(borrowRecordsService.returnBook(recordId)){
            return Result.success("还书成功！");
        }else{
            return Result.error("还书失败！");
        }
    }
    @PutMapping("/continueborrow")
    public Result continueBorrow(@RequestParam Integer recordId) {
        if(borrowRecordsService.continueBorrow(recordId)){
            return Result.success("续借成功！");
        }else{
            return Result.error("续借失败！");
        }
    }
    //借阅查找接口
    @GetMapping("/searchborrow")
    public Result searchBorrow(@RequestParam String info) {
        if(!borrowRecordsService.searchBorrow(info).isEmpty()){
            return Result.success(borrowRecordsService.searchBorrow(info));
        }else{
            return Result.error("没有数据2！");
        }
    }
}